require 'magnolia'

# This is a test account I set up. Please do not use this key or mess with the test account. Thanks.
Magnolia.new('fc18b7bd1caf67b06b16a462b0b3c00890304ad9')

puts '---Testing the adding of a bookmark...'
begin
  marks = Magnolia.add(:title => 'Addicted To New', 
                      :description => 'A really cool site.',
                      :tags => 'me, addicting, cool',
                      :rating => 5,
                      :url => 'http://addictedtonew.com/')
  addicted_mark = marks[0]
  puts addicted_mark.title
rescue Magnolia::RequestError => msg
  puts msg
  marks = Magnolia.search(:url => 'http://addictedtonew.com/')
  addicted_mark = marks[0] if marks.size > 0
end

puts
puts '---Testing the updating of a bookmark...'
marks = Magnolia.update(:id => addicted_mark.id, :title => 'Addicted To New by John Nunemaker')
markadd = marks[0]
puts markadd.title

puts
puts '---Testing Mark#save...'
begin
  markol = Magnolia::Mark.new
  markol.title  = 'Ordered List'
  markol.url    = 'http://orderedlist.com'
  markol.tags   = 'steve smith, coding'
  markol.save
  puts 'new mark id: ' + markol.id
  markol.title  = 'Ordered List by Steve Smith'
  puts 'Tags are: ' + markol.tags.inspect
  markol.tags = nil
  markol.save
  marks       = Magnolia.find(:id => markol.id)
  upmark      = marks[0]
  puts 'Title updated to: ' + upmark.title
  puts 'Tags are: ' + upmark.tags.inspect
rescue Magnolia::RequestError => msg
  puts msg
  marks = Magnolia.search(:url => 'http://orderedlist.com')
  markol = marks[0] if marks.size > 0
end

puts
puts '---Testing the find method ...'
begin
  marks = Magnolia.search(:person => 'magnoliaapitest')
  marks.each do |mark|
    puts mark.title, " #{mark.id}"
  end
rescue Magnolia::RequestError => msg
  puts msg
end

puts
puts '---Testing the get method ...'
marks = Magnolia.find(:id => addicted_mark.id)
marks.each do |mark|
  puts mark.img_tag
  puts mark.a_tag
end

puts
puts '---Testing the Mark#find method ...'
mark = Magnolia::Mark.find(addicted_mark.id)
puts mark.title

puts
puts '---Testing the adding of a tag...'
Magnolia.add_tags(:id => addicted_mark.id, :tags => 'john nunemaker')
marks = Magnolia.find(:id => addicted_mark.id)
puts marks[0].tags.inspect

Magnolia.delete_tags(:id => addicted_mark.id, :tags => 'john nunemaker')
marks = Magnolia.find(:id => addicted_mark.id)
puts marks[0].tags.inspect

puts
puts '---Testing the renaming of a tag...'
Magnolia.rename_tags(:id => 'all', :old => 'addicting', :new => 'very addicting')
marks = Magnolia.find(:id => addicted_mark.id)
puts marks[0].tags.inspect
Magnolia.rename_tags(:id => 'all', :old => 'very addicting', :new => 'addicting')
marks = Magnolia.find(:id => addicted_mark.id)
puts marks[0].tags.inspect

puts
puts '---Testing the deleting of addicted bookmark...'
Magnolia.delete(:id => addicted_mark.id)
puts '---Testing the deleting of ol bookmark...'
Magnolia.delete(:id => markol.id)

puts "---Testing the finding of a person's tags..."
tags = Magnolia.tags_find(:person => 'jnunemaker')
puts "Getting all of jnunemaker's tags...Total: #{tags.size}"
puts "Printing first ten tags..."
tags[0..10].each do |tag|
  puts "#{tag.name} #{tag.count}"
end

puts
puts '---End of tests'